#!/bin/sh

# example init script for dispatch.fcgi server
#
# chkconfig: 2345 80 20
# description: dispatch.fcgi - a remote fastcgi server for
#              extmail FCGI support
#
# processname: dispatch.fcgi
# pidfile: /var/run/dispatch.pid

# startup parameters
MAXFORK=5
MAXREQS=15
TIMEOUT=120
PORT=8888
SU_UID=vuser
SU_GID=vgroup

PATH=/bin:/usr/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/local/sbin
BASE=/var/www/extsuite/extmail
BIN=dispatch.fcgi
PID_FILE=/var/run/$BIN.pid

export PATH=$PATH

case "$1" in
'start')
	if [ ! -f $PID_FILE ]; then
		touch $PID_FILE
		chown $SU_UID:$SU_GID $PID_FILE
	else
		PID=`cat $PID_FILE`;
		kill -0 $PID
		if [ "$?" -eq 0 ];then
			echo "Pid of $PID is running, abort..."
			exit 255
		fi
	fi
	echo "Starting extmail server...";
	nice -19 $BASE/$BIN --port=$PORT --child=$MAXFORK --server --host=127.0.0.1 \
		--uid=$SU_UID --gid=$SU_GID --pid=$PID_FILE \
		--request=$MAXREQS --timeout=$TIMEOUT
	;;

'stop')
	if [ -f $PID_FILE ]; then
		echo "Stopping extmail server...";
		kill -TERM `cat $PID_FILE`
		rm $PID_FILE
	else
		echo "Extmail server not running or pid not found";
	fi
	;;

'restart')
	$0 stop
	$0 start
	;;

*)
	echo "Usage: $0 { start | restart | stop }"
	exit 1
	;;

esac
exit 0
